package com.cloud.pikachu.storage.task;

import com.cloud.pikachu.storage.config.LocalOssConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 刷新本地保存图片Oss配置信息
 *
 * @author : ABug
 * @Date : 2019/4/30 16:42
 * @UpdateDate : 2019/4/30 16:42
 * @Version V1.0.0
 **/
@Slf4j
@Component
public class RefreshLocalOssConfigTask {

    @Value("${oss.local.basePath}")
    private String basePath;

    @Autowired
    private LocalOssConfig localOssConfig;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd/HH");

    /**
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 60000)
    public void task() {
        String newFilePath = simpleDateFormat.format(new Date()) + File.separator;
        if(localOssConfig.newImageSavePath == null || !localOssConfig.newImageSavePath.equals(newFilePath)){
            //创建新的文件夹
            File file = new File(basePath + newFilePath);

            if(!file.exists()){
                file.mkdirs();
            }

            localOssConfig.newImageSavePath = newFilePath ;
            log.info("定时任务===========更新当前本地保存文件的文件夹" + newFilePath);
        }
    }
}
